﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Drawing.Imaging;
using Editor.Custom_Controls;

namespace Editor.Old
{
    public class PageImageBox : PageControl
    {
        public PageImageBox()
        {
        }

        public bool IsLong { get; set; }
        public bool IsFormula { get; set; }
        public Size ScrollableSize { get; set; }
        public int ScrollSize { get; set; }

        [XmlIgnore]
        public Image Image { get; set; }

        [XmlElement("Image")]
        public byte[] ImageSerialized
        {
            get
            { 
                if (Image == null) return null;

                using (MemoryStream ms = new MemoryStream())
                {
                    Image.Save(ms, ImageFormat.Png);
                    return ms.ToArray();
                }
            }
            set
            { 
                if (value == null)
                    Image = null;
                else
                {
                    using (MemoryStream ms = new MemoryStream(value))
                    {
                        Image = new Bitmap(ms);
                    }
                }
            }
        }

        public override string NamingPrefix
        {
            get { return "resim"; }
        }

        public override void UpdateInnerControl()
        {
            ScrollableImagePanel imagePanel = InnerControl as ScrollableImagePanel;

            imagePanel.Location = Location;
            imagePanel.Visible = Visible;
            imagePanel.Picture = Image;
            imagePanel.Size = Size;
            imagePanel.ScrollableSize = ScrollableSize;

            if (IsLong)
                imagePanel.ScrollSize = ScrollSize;
            else
                imagePanel.ScrollSize = 0;
        }

        protected override PropertiesFormBase generatePropertiesForm()
        {
            return new ImageBoxProperties();
        }

        protected override Control generateInnerControl()
        {
            return new ScrollableImagePanel();
        }

        public override object Clone()
        {
            PageImageBox newPageImageBox = new PageImageBox();

            newPageImageBox.Location = Location;
            newPageImageBox.Size = Size;
            newPageImageBox.Name = Name;
            newPageImageBox.Visible = Visible;

            newPageImageBox.Image = Image;
            newPageImageBox.IsLong = IsLong;
            newPageImageBox.IsFormula = IsFormula;
            newPageImageBox.ScrollableSize = ScrollableSize;

            return newPageImageBox;
        }
    }
}
